package com.ushine.web.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ushine.web.base.model.relation.RoleAndDataPermissionDO;
import com.ushine.web.base.model.relation.RoleAndFunctionPermissionDO;
import com.ushine.web.base.model.system.FunctionPermissionDO;
import com.ushine.web.base.model.system.RoleDO;
import com.ushine.web.base.service.IFunctionPermissionService;
import com.ushine.web.base.service.IRoleAndDataPermissionService;
import com.ushine.web.base.service.IRoleAndFunPermissionService;
import com.ushine.web.base.service.IRoleService;
import com.ushine.web.component.http.HttpResponseVO;
import com.ushine.web.component.page.PageRequestQuery;
import com.ushine.web.component.page.PageResultVO;
import com.ushine.web.log.annotation.SystemControllerLog;
import com.ushine.web.log.constant.LogConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: yls
 * @Date: 2021/3/15 9:23
 * @Description: 角色管理 -- 控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("system/role")
@Api(tags = "基础模块-角色管理")
@Validated
public class RoleController {

    @Resource
    private IRoleService roleService;

    @Resource
    private IFunctionPermissionService permissionService;

    @Resource
    private IRoleAndFunPermissionService roleAndFunPermissionService;

    @Resource
    private IRoleAndDataPermissionService roleAndDataPermissionService;

    @SystemControllerLog(operation = LogConstant.CREATE)
    @ApiOperation(value = "添加角色")
    @PostMapping("insert")
    public HttpResponseVO<Object> insertRole(@Validated @ApiParam(value = "角色对象", required = true) @RequestBody RoleDO role) {
        RoleDO roleByName = roleService.getRoleByName(role.getName());
        if (roleByName != null) {
            return new HttpResponseVO<>().error("添加失败，角色名称已存在！");
        }
        Boolean flag = roleService.insertRole(role);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("保存角色成功！");
        } else {
            return new HttpResponseVO<>().error("保存失败！");
        }
    }

    @SystemControllerLog(operation = LogConstant.DELETE)
    @ApiOperation(value = "删除角色", notes = "根据id批量删除")
    @DeleteMapping("deleteBatchRoles")
    public HttpResponseVO<Object> deleteBatchRoles(@ApiParam(value = "待删除的角色集合", required = true) @RequestBody List<String> roleIds) {
        Boolean flag = roleService.deleteRoles(roleIds);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("删除成功！");
        } else {
            return new HttpResponseVO<>().error("删除失败！");
        }
    }

    @SystemControllerLog(operation = LogConstant.READ)
    @ApiOperation(value = "查询所有角色", notes = "全表查询，不带任何条件")
    @PostMapping("listRoleByPage")
    public HttpResponseVO<PageResultVO<RoleDO>> listRoleByPage(@ApiParam(value = "查询用户条件", required = true) @RequestBody PageRequestQuery<RoleDO> pageRequestQuery) {
        Page<RoleDO> rolePage = roleService.listRolesByPage(pageRequestQuery);
        return new HttpResponseVO<PageResultVO<RoleDO>>().ok("查询所有的角色成功！", rolePage);
    }

    @SystemControllerLog(operation = LogConstant.READ)
    @ApiOperation(value = "查询单个角色", notes = "根据角色ID查询")
    @GetMapping("getRole")
    public HttpResponseVO<RoleDO> getRole(@ApiParam(value = "角色ID", required = true) @RequestParam String roleId) {
        RoleDO role = roleService.getRoleById(roleId);
        return new HttpResponseVO<RoleDO>().ok("查询角色成功！", role);
    }

    @SystemControllerLog(operation = LogConstant.READ)
    @ApiOperation(value = "查询角色的权限", notes = "根据角色ID查询")
    @PostMapping("listRolePermissions")
    public HttpResponseVO<List<FunctionPermissionDO>> listRolePermissions(@ApiParam(value = "角色ID", required = true) @RequestParam Long roleId) {
        Set<Long> roleIds = new HashSet<>();
        roleIds.add(roleId);
        List<FunctionPermissionDO> permissions = permissionService.listPermissonsByRoleIds(roleIds);
        return new HttpResponseVO<List<FunctionPermissionDO>>().ok("查询角色的权限成功！", permissions);
    }

    @SystemControllerLog(operation = LogConstant.CREATE)
    @ApiOperation(value = "保存角色的功能权限")
    @PostMapping("insertRoleFunPermissions")
    public HttpResponseVO<Object> insertRoleFunPermissions(@ApiParam(value = "角色权限关联表", required = true) @RequestBody List<RoleAndFunctionPermissionDO> roleAndPermissions) {
        Boolean flag = roleAndFunPermissionService.insertRoleFunPermission(roleAndPermissions);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("保存角色的功能权限成功！");
        } else {
            return new HttpResponseVO<>().ok("保存角色的功能权限失败！");
        }

    }

    @SystemControllerLog(operation = LogConstant.CREATE)
    @ApiOperation(value = "保存角色的数据权限")
    @PostMapping("insertRoleDataPermissions")
    public HttpResponseVO<Object> insertRolePermissions(@ApiParam(value = "角色权限关联表", required = true) @RequestBody List<RoleAndDataPermissionDO> roleAndDataPermissions) {
        Boolean flag = roleAndDataPermissionService.insertBatchRoleAndDataPermissions(roleAndDataPermissions);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("保存角色的数据权限成功！");
        } else {
            return new HttpResponseVO<>().ok("保存角色的数据权限失败！");
        }

    }
}
